import { useRouter } from "vue-router";
let $Router = useRouter();
export default {
  namespaced: true,
  state: {
    userInfo: {
      name: "张三",
      age: "35",
      sex: "男",
    },
    userList: [
      {
        id: 1,
        name: "张三",
        gender: "男",
        role: "班长",
        class_name: "云计算221",
        phone_number: "123456789",
        email: "xxxxx@126.com",
        userName: "admin",
        passWord: "123456",
      },
      {
        id: 2,
        name: "张三",
        gender: "男",
        role: "团支书",
        class_name: "云计算221",
        phone_number: "123456789",
        email: "xxxxx@126.com",
        userName: "user001",
        passWord: "123456",
      },
      {
        id: 3,
        name: "张三",
        gender: "男",
        role: "纪委",
        class_name: "云计算221",
        phone_number: "123456789",
        email: "xxxxx@126.com",
        userName: "user002",
        passWord: "123456",
      },
      {
        id: 4,
        name: "张三",
        gender: "男",
        role: "",
        class_name: "云计算221",
        phone_number: "123456789",
        email: "xxxxx@126.com",
        userName: "user003",
        passWord: "123456",
      },
    ],
    isLogin: false,
  },
  mutations: {
    changeisLogin(state) {
      state.isLogin = !state.isLogin;
    },
    setUserInfo(state, name) {
      state.name = name;
    },
    getFindUser(state, user) {
      console.log(user);
      let localUser = state.userList.find(
        (item) => item.userName === user.userName
      );
      console.log(localUser, "");

      if (localUser) {
        if (localUser.passWord === user.passWord) {
          state.userInfo = localUser;
          console.log("用户" + localUser + "登录成功。");
          state.isLogin = true;
          return;
        }
      }

      state.isLogin = false;
    },
    addUser(state, user) {
      //   state.userList.push(user);
      let localUser = state.userList.find(
        (item) => item.userName === user.userName
      );
      if (localUser) {
        console.log("已经存在同名的用户:", localUser);
        return false;
      } else {
        const nextId = Math.max(...state.userList.map((item) => item.id)) + 1;
        // console.log(nextId)
        state.userList.push({
          userName: user.userName,
          passWord: user.passWord,
          id: nextId,
        });
        state.userInfo = user;
        return true;
      }
    },
  },
  actions: {},
};
